﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KC.Presentation.Mv8CMS
{
	public partial class SpiderDetector : Form
	{
		string cs = System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString;
		public SpiderDetector()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void SpiderDetector_Load(object sender, EventArgs e)
		{
			timer1.Start();
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			DataTable dt = KC.DataAccess.Global.Global.CallSql_Query(cs, "update activities set isDetectionComplete=1 where id in (select distinct top 50 a.id from activities a inner join sessions s on a.sid=s.id inner join ips i on s.ipid=i.id where isspider=1 order by id desc); "+
				"select top 50 IPID, isSpider, a.* from activities a inner join sessions s on a.sid=s.id inner join ips i on s.ipid=i.id where address not like '192.168.%' and address not like '10.%' and a.datetime>'2012-02-01' order by isdetectioncomplete, id desc");
			for (int i=0; i < dt.Rows.Count; i++) {
				bool isSpider = false;
				string useragent = dt.Rows[i]["useragent"].ToString().ToLower().Trim();
				if (useragent.Contains("spider")
					|| useragent.Contains("bot")
					|| useragent.Contains("google")
					|| useragent.Contains("networkquality")
					|| useragent.Contains("ichiro")
					|| useragent.Contains("longurl")
					|| useragent.Contains("resolver")
					|| useragent.Contains("crawler")
					|| useragent.Contains("fetchor")
					|| useragent.Contains("summify")
					|| useragent.Contains("wget")
					|| useragent.Contains("facebookexternalhit")
					|| useragent.Contains("python-httplib2")
					|| useragent.Contains("api")
					|| useragent.Contains("ning/")
					|| useragent.Contains("postrank")
					|| useragent.Contains("eventmachine")
					|| useragent.Contains("readability")
					|| useragent.Contains("postrank")
					|| useragent.Contains("miner")
					|| useragent.Contains("ruby")
					|| useragent.Contains("untiny")
					|| useragent.Contains("scraper")
					|| useragent.Contains("crowsnest")
					|| useragent.Contains("nojs")
					|| useragent.Contains("twikle")
					|| useragent.Contains("getter")
					|| useragent.Contains("python")
					|| useragent.Trim().Length == 0
					)
					isSpider = true;

				KC.DataAccess.Global.Global.CallSql_Query(cs, "update activities set isDetectionComplete=1 where id=" + dt.Rows[i]["ID"].ToString());
				if (isSpider) KC.DataAccess.Global.Global.CallSql_Query(cs, "update IPs set isSpider=1 where id=" + dt.Rows[i]["IPID"].ToString());
			}

			dgvSpiders.DataSource = dt;
			dgvSpiders.Columns["ID"].Visible = false;
			dgvSpiders.Columns["PID"].Visible = false;
			dgvSpiders.Columns["SID"].Visible = false;
			dgvSpiders.Columns["mv5id"].Visible = false;
			dgvSpiders.Columns["notes"].Visible = false;
			dgvSpiders.Columns["isdetectioncomplete"].Visible = false;
			dgvSpiders.Columns["IPID"].Visible = false;
			dgvSpiders.Columns["isSpider"].Visible = false;

			dgvSpiders.Columns["request"].Width = 250;
			dgvSpiders.Columns["datetime"].Width = 90;
			dgvSpiders.Columns["referrer"].Width = 200;

			foreach (DataGridViewColumn col in dgvSpiders.Columns)
				col.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dgvSpiders.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
			dgvSpiders.DefaultCellStyle.Font = new System.Drawing.Font(FontFamily.GenericSerif, 7);
			dgvSpiders.Columns["UserAgent"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

			foreach (DataGridViewRow dr in dgvSpiders.Rows) {
				DataRowView drv = (DataRowView)dr.DataBoundItem;
				if ((bool)drv["isSpider"]) {
					dr.DefaultCellStyle.BackColor = Color.Black;
					dr.DefaultCellStyle.ForeColor = Color.White; } }
		}
	}
}
